package keeto.com.vn.classified.utils;

import keeto.com.vn.classified.config.AdBannerConfig;
import keeto.com.vn.classified.ui.MainApplication;
import android.util.Log;

/**
 * 
 * Centralized Configure Util for all picture size
 * 
 */
public class PictureSizeUtils {

	private static final String TAG = "PictureSizeUtils";
	
	/**
	 * Small Size used in SNS picture's thumbnail
	 */
	public static final int PICTURE_SIZE_SMALL = 2;
	/**
	 * The size is for SNS picture's preview size
	 */
	public static final int PICTURE_SIZE_MIDDLE = 1;
	/**
	 * the large size is for the Picture full screen view.
	 */
	public static final int PICTURE_SIZE_LARGE = 0;

	/**
	 * hdpi - L 480 x 760 M 297 x 470 L: 120 x 190 mdpi - L 320 x 456 M 198 x
	 * 282 L: 80 x 114 ldpi - L 240 x 302 M 148 x 187 L: 60 x 76
	 */
	private static final int[][] PICTURE_SIZE_MATRIX = new int[][] {
			{ 600, 760 }, { 384, 608 }, { 120, 190 }, 
			{ 320, 456 }, { 256, 365 }, { 80, 114 }, 
			{ 240, 302 }, { 192, 242 }, { 60, 76 } 
	};
	
	
	private static final int[][] IM_SEND_PICTURE_MATRIX = new int[][]{
		{ 600, 950 },
		{ 400, 570 },
		{ 300, 378 }
	};
	
	public static final int[][] IA_PICTURE_MATRIX = new int[][]{
		{170,100}, {120, 80}, {74, 54}
	};

	/*
	 * screen dpi
	 */
	private static final int QVGA = 320 * 240;
	private static final int HVGA = 480 * 320;
	private static final int WVGA = 800 * 480;
	private static final int XHVGA = 1024 * 600;

	/*
	 * build in avartar size
	 */
	private static int BUILDIN_AVARTAR_SMALL = 31;
	private static int BUILDIN_AVARTAR_MIDDLE = 41;
	private static int BUILDIN_AVARTAR_LARGE = 63;
	private static int BUILDIN_AVARTAR_XLARGE = 78;;
	
	/*
	 * sns avartar size
	 */
	private static int SNS_AVARTAR_SMALL = 31;
	private static int SNS_AVARTAR_MIDDLE = 41;
	private static int SNS_AVARTAR_LARGE = 63;
	private static int SNS_AVARTAR_XLARGE = 78;
	
	/*
	 * chat avartar size
	 */
	private static float CHAT_AVARTAR_SMALL = (float)19.2;
	private static float CHAT_AVARTAR_MIDDLE = (float)25.6;
	private static float CHAT_AVARTAR_LARGE = (float)38.4;
	private static float CHAT_AVARTAR_XLARGE = 48;
	
	/**
	 * max size for avartar upload
	 */
	public static final int MAX_AVARTAR_SIZE = 480;
	
	/** 
	 * quality for resize large picture
	 */
	public static final int QUALITY_RESIZE_LARGE_PICTURE = 60;

	/**
	 * Map's max size
	 */
	public static final int MAP_SIZE_WIDTH = MainApplication.getScreenWidth() - 20;
	public static final int MAP_SIZE_HEIGHT = MainApplication.getScreenHeight() - 60;


	/**
	 * get build-in roster avartar size according to the screen size
	 */
	public static int getImAvartarSize(int screenWidth, int secreenHeight) {

		int dpi = getDpiTypeByResolution(screenWidth, secreenHeight);

		switch (dpi) {
		case QVGA:
			return BUILDIN_AVARTAR_SMALL;
		case HVGA:
			return BUILDIN_AVARTAR_MIDDLE;
		case WVGA:
			return BUILDIN_AVARTAR_LARGE;
		case XHVGA:
			return BUILDIN_AVARTAR_XLARGE;
		default:
			return BUILDIN_AVARTAR_XLARGE;
		}
	}

//	/**
//	 * get SNS roster avartar size according to the screen size
//	 */
	public static int getSnsAvartarSize(int screenWidth, int screenHeight) {

		int dpi = getDpiTypeByResolution(screenWidth, screenHeight);

		switch (dpi) {
		case QVGA:
			return SNS_AVARTAR_SMALL;
		case HVGA:
			return SNS_AVARTAR_MIDDLE;
		case WVGA:
			return SNS_AVARTAR_LARGE;
		case XHVGA:
			return SNS_AVARTAR_XLARGE;
		default:
			return SNS_AVARTAR_XLARGE;
		}

	}

	/**
	 * 
	 * Get the most suitable image size for display pictures
	 * 
	 * 
	 * @param screenWidth
	 *            screen width
	 * @param ScreenHeight
	 *            screen height
	 * @param sizeId
	 *            the <b>small</b> size is used as thumbnail of SNS list, the
	 *            <b>middle</b> size is as the preview picture size, and the
	 *            <b>large</b> size is used for view larger picture.
	 *            {@link PictureSizeUtils#PICTURE_SIZE_SMALL},
	 *            {@link PictureSizeUtils#PICTURE_SIZE_MIDDLE},
	 *            {@link PictureSizeUtils#PICTURE_SIZE_LARGE}
	 * 
	 * @return the proper picture size for the <code>size</code>int[0] - width,
	 *         int[1] height
	 */
	public static int[] getSnsPictureDisplaySizeByScreenSize(int screenWidth,
			int screenHeight, final int sizeId) {

		int dpi = getDpiTypeByResolution(screenWidth, screenHeight);

		int hdpiStartIndex = 0;
		int mdpiStartIndex = 3;
		int ldpiStartIndex = 6;

		switch (dpi) {
		case QVGA:
			return PICTURE_SIZE_MATRIX[ldpiStartIndex + sizeId];
		case HVGA:
			return PICTURE_SIZE_MATRIX[mdpiStartIndex + sizeId];
		case WVGA:
			return PICTURE_SIZE_MATRIX[hdpiStartIndex + sizeId];
		case XHVGA:
			return PICTURE_SIZE_MATRIX[hdpiStartIndex + sizeId];
		default:
			return PICTURE_SIZE_MATRIX[hdpiStartIndex + sizeId];
		}
	}

	/**
	 * the matrix is used for resize facebook or twitter picture in the case of
	 * the picture size is larger than the screen size. we define the max border
	 * the picture can be use.
	 * 
	 * @param screenWidth
	 *            screen width
	 * @param screenHeight
	 *            screen height
	 * @return the resize border size for the scale function. int[0] - width,
	 *         int[1] - height
	 */
	public static int[] getSnsPictureDownloadResizeMatrix(int screenWidth,
			int screenHeight) {

		int dpi = getDpiTypeByResolution(screenWidth, screenHeight);

		int hdpiStartIndex = 0;
		int mdpiStartIndex = 3;
		int ldpiStartIndex = 6;

		switch (dpi) {
		case QVGA:
			return PICTURE_SIZE_MATRIX[ldpiStartIndex];
		case HVGA:
			return PICTURE_SIZE_MATRIX[mdpiStartIndex];
		case WVGA:
			return PICTURE_SIZE_MATRIX[hdpiStartIndex];
		case XHVGA:
			return PICTURE_SIZE_MATRIX[hdpiStartIndex];
		default:
			return PICTURE_SIZE_MATRIX[hdpiStartIndex];
		}
	}

	/**
	 * @return max size of border for update your own avartar
	 */
	public static int getUploadAvartarMaxSize() {
		return MAX_AVARTAR_SIZE;
	}

	
	/**
	 * 
	 * @param screenWidth
	 * @param screenHeight
	 * @return
	 */
	public static int[] getIMSendPictureMaxSize(int screenWidth,
			int screenHeight) {
		int dpi = getDpiTypeByResolution(screenWidth, screenHeight);

		int hdpiStartIndex = 0;
		int mdpiStartIndex = 1;
		int ldpiStartIndex = 2;

		switch (dpi) {
		case QVGA:
			return IM_SEND_PICTURE_MATRIX[ldpiStartIndex];
		case HVGA:
			return IM_SEND_PICTURE_MATRIX[mdpiStartIndex];
		case WVGA:
			return IM_SEND_PICTURE_MATRIX[hdpiStartIndex];
		case XHVGA:
			return IM_SEND_PICTURE_MATRIX[hdpiStartIndex];
		default:
			return IM_SEND_PICTURE_MATRIX[hdpiStartIndex];
		}
	}
	
	
	/**
	 * 
	 * @param screenWidth
	 * @param screenHeight
	 * @return
	 */
	public static int[] getSnsUploadPictureMaxSize(int screenWidth,
			int screenHeight) {
		int dpi = getDpiTypeByResolution(screenWidth, screenHeight);

		int hdpiStartIndex = 0;
		int mdpiStartIndex = 1;
		int ldpiStartIndex = 2;

		switch (dpi) {
		case QVGA:
			return IM_SEND_PICTURE_MATRIX[ldpiStartIndex];
		case HVGA:
			return IM_SEND_PICTURE_MATRIX[mdpiStartIndex];
		case WVGA:
			return IM_SEND_PICTURE_MATRIX[hdpiStartIndex];
		case XHVGA:
			return IM_SEND_PICTURE_MATRIX[hdpiStartIndex];
		default:
			return IM_SEND_PICTURE_MATRIX[hdpiStartIndex];
		}
	}

	public static int getDpiTypeByResolution(int screenWidth, int screenHeight) {

		int square = screenWidth * screenHeight;

		if (square > 1) {

			if (square <= QVGA) {
				return QVGA;
			} else if (square > QVGA && square < WVGA) {
				return HVGA;
			} else if (square >= WVGA && square < XHVGA) {
				return WVGA;
			} else {
				return XHVGA;
			}

		} else {
			return WVGA;
		}

	}
	
	/**
	 * 
	 * @param screenWidth
	 * @param screenHeight
	 * @return
	 */
	public static int[] getImChatPictureThumbnailSize(int screenWidth, int screenHeight){
		
		int[] borders = getSnsPictureDisplaySizeByScreenSize(screenWidth,
				screenHeight, PICTURE_SIZE_SMALL);
		
		return borders;
		
	}
	
	/**
	 * for get SNS thumbnail picture url
	 */
	public static int[] getSnsThumbnailPictureSize(
			int screenWidth, int screenHeight) {
		int[] borders = getSnsPictureDisplaySizeByScreenSize(screenWidth,
				screenHeight, PICTURE_SIZE_SMALL);
		return borders;
	}


	public static int getAdBannerSize(int screenWidth, int screenHeight) {

		int square = screenWidth * screenHeight;

		if (square > 1) {

			if (square <= QVGA) {

				return AdBannerConfig.AD_SMALL;

			} else if (square > QVGA && square < WVGA) {

				return AdBannerConfig.AD_MEDIUM;

			} else if (square >= WVGA && square < XHVGA) {

				return AdBannerConfig.AD_LARGE;

			} else {

				return AdBannerConfig.AD_XLARGE;

			}

		} else {
			return AdBannerConfig.AD_LARGE;
		}
	}
	
	
	public static int[] getIAPictureSize(int screenWidth, int screenHeight){
		
		int dpi = getDpiTypeByResolution(screenWidth, screenHeight);
		switch (dpi) {
		case QVGA:
			return IA_PICTURE_MATRIX[2];
		case HVGA:
			return IA_PICTURE_MATRIX[1];
		case WVGA:
			return IA_PICTURE_MATRIX[0];
		case XHVGA:
			return IA_PICTURE_MATRIX[0];
		default:
			return IA_PICTURE_MATRIX[0];
		}
		
	}

	public static float getChatImageSize(int screenWidth, int screenHeight){
		int dpi = getDpiTypeByResolution(screenWidth, screenHeight);
		switch (dpi) {
		case QVGA:
			return CHAT_AVARTAR_SMALL;
		case HVGA:
			return CHAT_AVARTAR_MIDDLE;
		case WVGA:
			return CHAT_AVARTAR_LARGE;
		case XHVGA:
			return CHAT_AVARTAR_XLARGE;
		default:
			return CHAT_AVARTAR_LARGE;
		}
	}
}
